﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AuriaCompte.Lib.Dao;
using AuriaCompte.Lib;

namespace AuriaCompte.Fenetre
{
    public partial class GestionFichierCompteForm 
        : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public GestionFichierCompteForm()
        {
            InitializeComponent();
        }

        private void GestionFichierCompteForm_Load(object sender, EventArgs e)
        {
            DaoDb daoDb = new DaoDb();
            this.RempliForm(daoDb.Select());   
        }

        public void RempliForm(Db bd)
        {
            this.kCbUsePassword.Checked = bd.IsPasswordProtected;
            this.kTbPassword.Text = this.kTbPasswordConfirm.Text = bd.Password;
        }

        public Db RempliObjet()
        {
            Db retour = new Db();
            retour.Password = this.kTbPassword.Text;
            retour.IsPasswordProtected = this.kCbUsePassword.Checked;
            return retour;
        }
         

        public bool IsFormValid()
        {
            if (this.kCbUsePassword.Checked && this.kTbPassword.Text != this.kTbPasswordConfirm.Text)
            {
                ComponentFactory.Krypton.Toolkit.KryptonTaskDialog.Show("Erreur", "Mot de passe", "Les mots de passe saisies ne correspondent pas.", MessageBoxIcon.Error, ComponentFactory.Krypton.Toolkit.TaskDialogButtons.OK);
                return false;
            }
            return true;
        }

        private void kBtnValider_Click(object sender, EventArgs e)
        {
            if (this.IsFormValid())
            {
                DaoDb daobd = new DaoDb();
                daobd.Update(this.RempliObjet());
                this.Close();
            }
        }

        private void kBtnAnnuler_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
